<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Activity;


use App\Exports\UniversalExport;
use App\Http\Controllers\Controller;
use App\Models\ActivityLuck;
use App\Models\ActivityLuckDetail;
use App\Models\ActivityLuckDetailLog;
use App\Models\Order;
use App\Models\UserHome;
use App\Models\VipLog;
use App\Services\Manager\ExcelService;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class LuckController extends Controller
{
    private $namespace = "App\Http\Controllers\Api\V1\Manager\Activity\LuckController";

    public function index(Request $request)
    {
        $activityLuck = ActivityLuck::first();
        $luckList = ActivityLuckDetail::orderBy('sort', 'asc')->get()->toArray();
        foreach ($luckList as $key => $value) {
            $luckList[$key]['lunck_image'] = path($value['lunck_image']);
        }
        $activityLuck['luckList'] = $luckList;
        return $this->apiResponse($activityLuck);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $data = $request->only("title", 'integral', 'detail');
        if (!$data['title'] || !$data['integral'] || !$data['detail']) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        ActivityLuck::where('id', '!=', 0)->update($data);
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = "修改";
        $content = "修改了大转盘的标题为【" . $data['title'] . "】,规则为【".$data['detail'] . "】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "大转盘模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function update(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $activityLuck = ActivityLuck::first();
        if ($activityLuck['status']) {
            $content = "禁用";
            ActivityLuck::where('id', '!=', 0)->update(['status' => 0]);
        } else {
            $content = "启用";
            ActivityLuck::where('id', '!=', 0)->update(['status' => 1]);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/update";
        $title = "修改";
        $content .= "了大转盘";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "大转盘模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function luckDetail(Request $request)
    {
        $id = $request->id;
        $data = $request->only("lunck_name", 'lunck_image', 'lunck_type', 'lunck_cost');
        $integral = $request->integral ?: 0;
        $data['integral'] = $integral;
        if (!isset($data['lunck_name']) || !isset($data['lunck_image']) || !isset($data['lunck_type']) || !isset($data['lunck_cost'])) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        ActivityLuckDetail::where('id', $id)->update($data);
        //添加管理员日志开始
        $url = $this->namespace . "/luckDetail";
        $title = "修改";
        $content = "修改了id为【".$id."】大转盘奖励为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "大转盘模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function luckDetailList(Request $request)
    {
        $userId = $request->userId;
        $userPhone = $request->userPhone;
        $nickname = $request->nickname;
        $lunck_type = $request->lunck_type;
        $lunck_name = $request->lunck_name;
        $createStartTime = $request->createStartTime;
        $createEndTime = $request->createEndTime;
        $pageSize = $request->pageSize;
        $sql = ActivityLuckDetailLog::select('t1.id', 't1.userId', 't2.phone as userPhone', 't2.nickname', 't1.lunck_name', 't1.lunck_type', 't1.lunck_cost', 't1.createTime')
            ->from((new ActivityLuckDetailLog())->getTable() . ' as t1')
            ->leftJoin((new UserHome())->getTable() . ' as t2', 't1.userId', '=', 't2.id');
        $ids = $request->ids;
        $idList = [];
        if (isset($ids)) {
            $ids = explode(';', $ids);
            foreach ($ids as $v) {
                $idList[] = $v;
            }
        }
        if (isset($idList[0])) {
            $sql->whereIn('t1.id', $idList);
        }
        if (isset($userId)) {
            $sql->where('t1.userId', $userId);
        }
        if (isset($userPhone)) {
            $sql->where('t2.phone', 'like', "%$userPhone%");
        }
        if (isset($nickname)) {
            $sql->where('t2.nickname', 'like', "%$nickname%");
        }
        if (isset($lunck_type)) {
            $sql->where('t1.lunck_type', $lunck_type);
        }
        if (isset($lunck_name)) {
            $sql->where('t1.lunck_name', 'like', "%$lunck_name%");
        }
        if (isset($createStartTime)) {
            $sql->where('t1.createTime', '>=', $createStartTime." 00:00:00");
        }
        if (isset($createEndTime)) {
            $sql->where('t1.createTime', '<=', $createEndTime." 23:59:59");
        }
        if (isset($request->excel)) {
            $luckDetailList = $sql->orderByDesc('t1.createtime')
                ->get()
                ->toArray();
            $title = ['用户id', '用户手机号', '用户名称', '奖品名称', '奖品类型', '奖品成本', '中奖时间'];
            $data = [];
            $lunckType = ['谢谢惠顾', '积分', '商品'];
            foreach ($luckDetailList as $value) {
                $luckDetail = [];
                $luckDetail[] = $value['userId'];
                $luckDetail[] = $value['userPhone'];
                $luckDetail[] = $value['nickname'];
                $luckDetail[] = $value['lunck_name'];
                $luckDetail[] = $lunckType[$value['lunck_type']];
                $luckDetail[] = $value['lunck_cost'];
                $luckDetail[] = $value['createTime'];
                $data[] = $luckDetail;
            }
            //return var_dump($data);
            return Excel::download(new UniversalExport($data,$title), "大转盘中奖明细".date("ymdHis").'.xlsx');
        }
        $luckDetailList = $sql->orderByDesc('t1.createtime')
            ->paginate($pageSize)
            ->toArray();
        $data['luckDetailList'] = $luckDetailList['data'];
        $data['totalNum'] = $luckDetailList['total'];
        return $this->apiResponse($data);
    }
}